<template>
  <PageWrapper class="high-form">
    <CollapseContainer title="查询条件">
      <BasicForm @register="registerForm">
      </BasicForm>
    </CollapseContainer>

    <CollapseContainer class="!mb-4">
      <BasicTable :title="title" :columns="remoteColumns1" :dataSource="remoteData1">
      </BasicTable>
    </CollapseContainer>

  </PageWrapper>
</template>

<script lang="ts">
import { defineComponent, ref, onMounted, } from 'vue';
import { BasicForm, useForm, } from '/@/components/Form/index';
import { PageWrapper } from '/@/components/Page';
import { CollapseContainer } from '/@/components/Container';
import { BasicTable, } from '/@/components/Table';
import { BasicColumn } from '/@/components/Table/src/types/table';

import { capitalSummary, } from '/@/api/business/summary';
import { formSchema } from './data';

export default defineComponent({
  components: { PageWrapper, CollapseContainer, BasicForm, BasicTable, },
  setup() {

    //查询
    const search = async () => {
      remoteColumns1.value = []
      remoteData1.value = []

      const params = await validate();
      if (!params.queryType) {
        params.queryType = 1
      }

      if (params.queryType == 1) {
        title.value = '个人业绩统计(明细)'
        remoteColumns1.value.push({
          title: '用户',
          dataIndex: 'columnUser',
        })
      } else {
        title.value = '部门业绩统计(明细)'
        remoteColumns1.value.push({
          title: '部门',
          dataIndex: 'columnDept',
        })
      }

      //查询个人业绩统计
      capitalSummary(params).then((res) => {
        // 处理返回的列名

        res.columnList.forEach((columnName, columnIndex) => {
          remoteColumns1.value.push({
            title: columnName,
            dataIndex: 'column' + columnIndex,
          })
        })
        // 处理返回的数据
        res.dataList.forEach((item, i) => {
          let data = {}
          data['columnUser'] = res.userList[i]
          data['columnDept'] = res.deptList[i]
          res.columnList.forEach((_, columnIndex) => {
            data['column' + columnIndex] = item[columnIndex]
          })
          remoteData1.value.push(data)
        })
        console.log('remoteData', remoteData1.value)
      })
      //end
    }

    //查询表单
    const [registerForm, { validate }] = useForm({
      schemas: formSchema,
      labelWidth: 100,
      showActionButtonGroup: true,
      submitFunc: search,
    });

    onMounted(() => {
      //默认查询
      search();
    });

    const title = ref<any>('')
    const remoteData1 = ref<any>([])
    const remoteColumns1 = ref<BasicColumn[]>([])

    return {
      registerForm, remoteColumns1, remoteData1, title,
    }
  },
});
</script>
